package com.informationsystem.model;

import com.informationsystem.model.BaseObject;
import com.informationsystem.service.DepartmentManager;

import org.apache.commons.lang.StringEscapeUtils;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Transient;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;

import javax.xml.bind.annotation.XmlRootElement;

import java.io.Serializable;

@Entity
@Table(name="notice")
@Indexed
@XmlRootElement
public class Notice extends Information implements Serializable {
    private Department department;
    private String content;
    private Set<Attachement> attachements = new HashSet<Attachement>(0);
    
@ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="department_id")
    public Department getDepartment() {
        return this.department;
    }
    
    public void setDepartment(Department department) {
        this.department = department;
    }
    
    @Column(name="content", nullable=false, length=10000)
    @Field
    public String getContent() {
        return this.content;
    }
    
    public void setContent(String content) {
        this.content = content;
    }
	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="notice")
	@Fetch(value = FetchMode.SUBSELECT)
    public Set<Attachement> getAttachements() {
        return this.attachements;
    }
    
    public void setAttachements(Set<Attachement> attachements) {
        this.attachements = attachements;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Notice pojo = (Notice) o;

        if (department != null ? !department.equals(pojo.department) : pojo.department != null) return false;
        if (content != null ? !content.equals(pojo.content) : pojo.content != null) return false;

        return true;
    }

    public int hashCode() {
        int result = 0;
        result = 31 * result + (department != null ? department.hashCode() : 0);
        result = 31 * result + (content != null ? content.hashCode() : 0);

        return result;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer(getClass().getSimpleName());

        sb.append(" [");
        sb.append("id").append("='").append(getId()).append("', ");
        sb.append("department").append("='").append(getDepartment()).append("', ");
        sb.append("content").append("='").append(getContent()).append("', ");
        
        sb.append("]");
      
        return sb.toString();
    }
    
	@Transient
	public String getDisplayContent(){
		return StringEscapeUtils.unescapeHtml(content);
	}

}
